package com.rambo.baselib.bean;

import com.rambo.baselib.ResourceTable;
import ohos.agp.components.Component;
import ohos.agp.components.Image;
import ohos.agp.components.ListContainer;
import ohos.agp.components.Text;
import ohos.media.image.PixelMap;

import java.util.HashMap;
import java.util.Map;

/***********************************************************************
 * <p>@description: toolbar holder
 * <p>@author: rambo
 * <p>@version: 1.0.0
 * <p>@created on: 2022/1/17 10:55
 **********************************************************************/
public class ToolbarHolder {
    private Map<Integer, Component> viewMap;
    private Component toolbar;

    public ToolbarHolder() {

        viewMap = new HashMap<>();
    }

    public void bindToolbarHolder(Component toolbar) {
        this.toolbar = toolbar;
        setPixelMap(ResourceTable.Id_toolbar_iv_back, ResourceTable.Media_back_white_24);
    }

    public <T> T getView(int id) {
        Component component = viewMap.get(id);
        if (component == null) {
            component = toolbar.findComponentById(id);
            viewMap.put(id, component);

        }
        if (component == null) return null;
        return (T) component;
    }

    public ToolbarHolder setDefaultTitle(String value) {
        Text text = getView(ResourceTable.Id_toolbar_title);
        if (text == null) return this;
        text.setText(value);
        return this;
    }

    public ToolbarHolder setDefaultTitle(int resId) {
        Text text = getView(ResourceTable.Id_toolbar_title);
        if (text == null) return this;
        text.setText(resId);
        return this;
    }

    public ToolbarHolder setDefaultRightOneText(String value) {
        Text text = getView(ResourceTable.Id_toolbar_text_right_one);
        if (text == null) return this;
        text.setVisibility(Component.VISIBLE);
        text.setText(value);
        return this;
    }
    public ToolbarHolder setDefaultRightOneTextClick(Component.ClickedListener listener) {
        setClickedListener(ResourceTable.Id_toolbar_text_right_two,listener);

        return this;
    }
    public ToolbarHolder setDefaultRightTwoText(String value) {
        Text text = getView(ResourceTable.Id_toolbar_text_right_two);
        if (text == null) return this;
        text.setVisibility(Component.VISIBLE);
        text.setText(value);
        return this;
    }
    public ToolbarHolder setDefaultRightTwoTextClick(Component.ClickedListener listener) {
        setClickedListener(ResourceTable.Id_toolbar_text_right_two,listener);

        return this;
    }
    public ToolbarHolder setText(int id, String value) {
        Text text = getView(id);
        if (text == null) return this;
        text.setText(value);
        return this;
    }

    public ToolbarHolder setText(int id, int resId) {
        Text text = getView(id);
        if (text == null) return this;
        text.setText(resId);
        return this;
    }

    public ToolbarHolder setVisibility(int id, boolean visible) {
        Component component = getView(id);
        if (component == null) return this;
        component.setVisibility(visible ? Component.VISIBLE : Component.HIDE);
        return this;
    }

    public ToolbarHolder setPixelMap(int id, int resId) {
        Image image = getView(id);
        if (image == null) return this;
        image.setPixelMap(resId);
        return this;
    }

    public ToolbarHolder setDefaultRightOneIcon(int resId) {
        Image image = getView(ResourceTable.Id_toolbar_iv_right_one);
        if (image == null) return this;
        image.setVisibility(Component.VISIBLE);
        image.setPixelMap(resId);
        return this;
    }

    public ToolbarHolder setDefaultRightOneIconClick(Component.ClickedListener listener) {
        setClickedListener(ResourceTable.Id_toolbar_iv_right_one, listener);
        return this;
    }

    public ToolbarHolder setDefaultRightTwoIcon(int resId) {
        Image image = getView(ResourceTable.Id_toolbar_iv_right_two);
        if (image == null) return this;
        image.setVisibility(Component.VISIBLE);
        image.setPixelMap(resId);
        return this;
    }
    public ToolbarHolder setDefaultRightTwoIcon(Component.ClickedListener listener) {
        setClickedListener(ResourceTable.Id_toolbar_iv_right_two, listener);
        return this;
    }
    public ToolbarHolder setPixelMap(int id, PixelMap pixelMap) {
        Image image = getView(id);
        if (image == null) return this;
        image.setPixelMap(pixelMap);
        return this;
    }

    public ToolbarHolder setClickedListener(int id, Component.ClickedListener listener) {
        Component component = getView(id);
        if (component == null) return this;
        component.setClickedListener(listener);
        return this;
    }
}
